\documentclass{article}

\begin{document}

<<>>=
sink("main_log_egypt.txt", append=F, type="output")
@


<<>>=
library(foreign)
library(cregg)

cjegypt = read.csv("Egypt.csv")
cjegypt <- subset(cjegypt, cjegypt$EGYPTNat != "Expat")

cjegypt$Elections <- factor(cjegypt$Elections)
cjegypt$Elections <- relevel(cjegypt$Elections, ref = "There are elections with multiple recognized political parties")
cjegypt$Participation <- factor(cjegypt$Participation)
cjegypt$Official_Religion <- factor(cjegypt$Official_Religion)
cjegypt$Role_Religion <- factor(cjegypt$Role_Religion)
cjegypt$Services <- factor(cjegypt$Services)
cjegypt$Unemployment_Rate <- factor(cjegypt$Unemployment_Rate)
@

<<Figure 2 & Appendix D>>=
f1 <- Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate

amces <- cj(cjegypt, f1, id = ~RecordNo)
pdf(file="figure2_egypt.pdf")
plot(amces, legend_pos=3)
dev.off()

pdf(file="appendixd_egypt.pdf")
plot(mm(cjegypt, f1, id = ~RecordNo), vline = 0.5, legend_pos=3)
dev.off()
@

<<Appendix C. College educated>>=
cjegypt$college <- 0
cjegypt$college[cjegypt$education == "University first degree (e.g. BA, BSc)"] = 1
cjegypt$college[cjegypt$education == "University higher degree (Masters, MBA, PhD)"] = 1
cjegypt$college[cjegypt$education == "Professional higher education (e.g. to qualify as a lawyer, accountant)"] = 1
cjegypt$college <- as.factor(cjegypt$college)

mm_diffs(cjegypt, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate, ~ college, id = ~ RecordNo)

pdf(file="appendixc_egypt.pdf")
plot(mm_diffs(cjegypt, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate, ~ college, id = ~ RecordNo),  legend_pos = "none")
dev.off()
@

<<Appendix E>>=
table(cjegypt$Unemployment_Rate)
appendixe <- cjegypt[ which(cjegypt$Unemployment_Rate=="High unemployment (14%)"),]
pdf(file="appendixe_high_egypt.pdf")
plot(cj(appendixe, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services , id = ~RecordNo), legend_pos = "none") #high unemployment side
dev.off()
appendixe <- cjegypt[ which(cjegypt$Unemployment_Rate=="Low unemployment (7%)"),]
pdf(file="appendixe_low_egypt.pdf")
plot(cj(appendixe, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services , id = ~RecordNo), legend_pos = "none") #low unemployment side
dev.off()
@

<<Appendix F>>=
table(cjegypt$Services)
appendixf <- cjegypt[ which(cjegypt$Services=="Good government provision of basic items (ie housing and food) to individuals"),]
pdf(file="appendixf_good_egypt.pdf")
plot(cj(appendixf, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Unemployment_Rate , id = ~RecordNo), legend_pos = "none") #good provision side
dev.off()
appendixf <- cjegypt[ which(cjegypt$Services=="Some government provision of basic items (ie housing and food) to individuals"),]
pdf(file="appendixf_some_egypt.pdf")
plot(cj(appendixf, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Unemployment_Rate , id = ~RecordNo), legend_pos = "none") #some provision side
dev.off()
appendixf <- cjegypt[ which(cjegypt$Services=="Little government provision of basic items (ie housing and food) to individuals"),]
pdf(file="appendixf_little_egypt.pdf")
plot(cj(appendixf, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Unemployment_Rate , id = ~RecordNo), legend_pos = "none") #little provision side
dev.off()
@

<<Appendix G>>=
table(cjegypt$Elections)
appendixg <- cjegypt[ which(cjegypt$Elections=="There are elections with multiple recognized political parties"),]
pdf(file="appendixg_multi_egypt.pdf")
plot(cj(appendixg, Choice ~ Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate , id = ~RecordNo), legend_pos = "none") #multi party side
dev.off()
appendixg <- cjegypt[ which(cjegypt$Elections=="There are elections with one recognized political party"),]
pdf(file="appendixg_one_egypt.pdf")
plot(cj(appendixg, Choice ~ Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate , id = ~RecordNo), legend_pos = "none") #one party side
dev.off()
appendixg <- cjegypt[ which(cjegypt$Elections=="There are no elections"),]
pdf(file="appendixg_none_egypt.pdf")
plot(cj(appendixg, Choice ~ Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate , id = ~RecordNo), legend_pos = "none") #no elections side
dev.off()
@

<<Appendix H>>=
cjegypt$dembest <- 0
cjegypt$dembest[cjegypt$Q8 == "An elected government is always preferable to any other kind of government"] = 1
cjegypt$dembest <- as.factor(cjegypt$dembest)

mm_diffs(cjegypt, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate, ~ dembest, id = ~ RecordNo, legend_pos = "none")

pdf(file="appendixh_egypt.pdf")
plot(mm_diffs(cjegypt, Choice ~ Elections + Participation + Official_Religion + Role_Religion + Services + Unemployment_Rate, ~ dembest, id = ~ RecordNo), legend_pos = "none")
dev.off()
@

<<Appendix B>>=
cj_props(cjegypt, ~ Elections, id = ~ CaseID)
cj_props(cjegypt, ~ Participation, id = ~ CaseID)
cj_props(cjegypt, ~ Official_Religion, id = ~ CaseID)
cj_props(cjegypt, ~ Role_Religion, id = ~ CaseID)
cj_props(cjegypt, ~ Services, id = ~ CaseID)
cj_props(cjegypt, ~ Unemployment_Rate, id = ~ CaseID)
@

<<>>=
sink() # final line here
@

\end{document}
